<template>
  <div class="login">
    <div class="center">
      <hips-button type="danger">危险按钮</hips-button>
      <hips-rate v-model="value3" show-score />

    </div>
    <form @submit.prevent="handleLogin">
      <input v-model="username" type="text" placeholder="用户名" required/>
      <br/>
      <input v-model="password" type="password" placeholder="密码" required/>
      <br/>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
import {setAuthCookie} from "@/utils/auth";
import JSEncrypt from "jsencrypt";
import {login} from '@/api/index'
export default {
  name: "LoginPage",
  data() {
    return {
      value3:1,
      username: "",
      password: "",
      publicKey: `-----BEGIN PUBLIC KEY-----
        YOUR_SERVER_PUBLIC_KEY_HERE
        -----END PUBLIC KEY-----`, // 替换 RSA 公钥   改
    };
  },
  methods: {
    handleLogin() {
      const encrypt = new JSEncrypt();
      let pwd = encrypt.setPublicKey(this.publicKey);
      if (pwd) {
        login({
          username: this.username,
          password: encrypt.encrypt(this.password)
        }).then(res => {
          //改
          if (res.code === 200) {
            // 如果登录成功，设置 cookie  放token
            setAuthCookie(res.data);
          } else {
            this.$hips.toast({
              message: res.msg,
              icon: 'success',
            })
          }
        })
      } else {
        console.log(123);

      }


      // const response = await this.$axios.post("/oauth/token", {
      //   username: this.username,
      //   password: encryptedPassword,
      // });

      // // 如果登录成功，设置 cookie
      // setAuthCookie(response.data.token);

      // // 跳转到首页
      // this.$router.push("/");
    },
  },
};
</script>
<style scoped lang="less">
.login {
  //display: flex;
  //justify-content: center;
  .center {
    width: 375px;
    height: 300px;
  }
}
</style>
